<?php
namespace APP\C;

class My extends Controller
{
    const MAX_CATA_COUNT = 100;

    protected $_email = '';
    protected $_cid = 0;

    public function init()
    {
        // 是否已登陆
        $this->_email = \SF\Session\Session::get(self::SESS_EMAIL);
        if (!$this->_email) {
            $this->router->redirect('/account/login');
        }
        $this->_cid = $this->request->getQuery('cid', 0);
    }


    public function indexAction()
    {
        $page = $this->request->getQuery('page', 1);
        $num = 30;
        $range = 10;

        $list = $this->getModel('url', $this->_email)->getList($page, $num, $this->_cid);
        $count = $this->getDb()->fetchOne('SELECT FOUND_ROWS()');

        if ($count > $num) {
            $pages = new \SF\Paginator($page, $count, $num, $range);
            $this->view->pages = $pages->getRender();
        }

        $this->view->urls = $list;
        $clist = $this->getModel('cata', $this->_email)->getList();
        $this->view->ufav_cnt = $this->getModel('user', $this->_email)->getFavCount();
        $this->view->uclist = $clist;
        $this->view->cname = ($this->_cid && isset($clist[$this->_cid])) ? $clist[$this->_cid]['name'] : '默认收藏夹';
        $this->view->render('my.tpl');
    }


    /**
     * 创建文件夹
     */
    public function addcataAction()
    {
        $input = new \APP\PUT\Addcata($this->request->getParam());
        $result = new \SF\Result();
        if ($input->isValid()) {
            $cataModel = $this->getModel('cata', $this->_email);
            // 达到创建上限
            if ($cataModel->getCount() >= self::MAX_CATA_COUNT) {
                $result->setMessage('最多只能创建'. self::MAX_CATA_COUNT .'个收藏夹');
            } else {
                $cid = $cataModel->create($input->newcata_name);
                $result->cid = $cid;
                $result->cname = $input->newcata_name;
                $result->setValid();
            }
        } else {
            $result->setMessage($input->getMessage());
        }
        echo $result->getByJson();
        exit;
    }


    public function addurlAction()
    {
        $input = new \APP\PUT\Addurl($this->request->getPost());
        $result = new \SF\Result();
        if ($input->isValid()) {
            $url = $input->url;
            $title = $input->title;
            $cata = $input->cata;
            // 保存url
            $urlModel = $this->getModel('url', $this->_email);
            $id = $urlModel->save($input->getData());
            $result->setValid();
            $result->title = $title;
            $result->url = $url;
            $result->id = $id;
        } else {
            $result->setMessage($input->getMessage());
        }
        echo $result->getByJson();
        exit;
    }


    public function editurlAction()
    {
        $input = new \APP\PUT\Editurl($this->request->getPost());
        $result = new \SF\Result();
        if ($input->isValid()) {
            // 保存url
            $urlModel = $this->getModel('url', $this->_email);
            $id = $urlModel->save($input->getData());
            $result->setValid();
            $result->title = $input->title;
            $result->url = $input->url;
            $result->intro = $input->intro;
            $result->id = $input->id;
        } else {
            $result->setMessage($input->getMessage());
        }
        echo $result->getByJson();
        exit;
    }


    public function delurlAction()
    {
        $id = $this->request->getParam('id', 0);
        if ($id) {
            $urlModel = $this->getModel('url', $this->_email);
            $urlModel->deleteById($id);
        }
        $this->router->redirect($this->request->getReferer());
    }


    public function saveintroAction()
    {
        $id = $this->request->getParam('id', 0);
        $intro = $this->request->getParam('intro', '');
        if ($id && $intro) {
            $this->getModel('url', $this->_email)->saveIntro($id, $intro);
        }
    }

    public function editcataAction()
    {
        $id = $this->request->getQuery('id', 0);
        $rename = $this->request->getQuery('rename', '');
        $result = new \SF\Result();
        if ($id && $rename) {
            $this->getModel('cata', $this->_email)->edit($id, $rename);
            $result->setValid();
        } else {
            $result->setMessage('修改失败，请刷新再试');
        }
        echo $result->getByJson();
        exit;
    }

    public function delcataAction()
    {
        $id = $this->request->getQuery('id', 0);
        $result = new \SF\Result();
        if ($id) {
            $this->getModel('cata', $this->_email)->delete($id, 'id', 1);
            $result->setValid();
        } else {
            $result->setMessage('删除失败，请刷新再试');
        }
        echo $result->getByJson();
        exit;
    }
}